﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace T01WPF
{
    /// <summary>
    /// SaveUserDialog.xaml 的交互逻辑
    /// </summary>
    public partial class SaveUserDialog : Window
    {
        public User user;
        public string ActionType { get; set; }
        public SaveUserDialog()
        {
            InitializeComponent();
        }
        public SaveUserDialog(User user):this()
        {
            this.user = user;
            Username.Text = user.Username;
            Password.Text = user.Password;
            Nickname.Text = user.Nickname;
            Is_Admin.IsChecked = user.Is_Admin;
        }
        private void SaveUser_Click(object sender, RoutedEventArgs e)
        {
            if(ActionType=="insert")
            {
                User _user = new User();
                _user.Username = Username.Text.Trim();
                _user.Password = Password.Text.Trim();
                _user.Nickname = Nickname.Text.Trim();
                _user.Is_Admin = Is_Admin.IsChecked==true?true:false;
                if(UserDao.Insert(_user)>0)
                {
                    this.DialogResult = true;
                    this.Close();
                }
            }
            else if(ActionType=="update")
            {
                user.Username = Username.Text.Trim();
                user.Password = Password.Text.Trim();
                user.Nickname = Nickname.Text.Trim();
                user.Is_Admin = Is_Admin.IsChecked == true ? true : false;
                UserDao.Update(user);
                this.DialogResult = true;
                this.Close();
            }
        }
    }
}
